//
//  VPTabbarViewController.m
//  VideoPlayer
//
//  Created by A on 2021/5/30.
//

#import "TabbarViewController.h"
#import "UIImage+YYAdd.h"

#import "MeViewController.h"
#import "HomeViewController.h"
#import "BaseNavigationViewController.h"
#import "SportViewController.h"
#import "HardwareViewController.h"

@interface TabbarViewController ()<UITabBarControllerDelegate>

@end

@implementation TabbarViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateTheme) name:@"k_change_theme" object:nil];

    
    self.delegate = self;
    // Do any additional setup after loading the view.
    [[UITabBar appearance] setBarTintColor:[UIColor whiteColor]];
    [[UITabBar appearance] setTintColor:[UIColor whiteColor] ];
    [UITabBar appearance].translucent = YES;//取消tabBar的透明效果
    
   
    
    NSArray *controllerList = @[
        [self createTabBarVCClassName:@"HomeViewController" vcTitle:LocalizedString(@"tab_home") defaultImage:@"首页 (2)" selectedImage:@"首页 (1)"],
        [self createTabBarVCClassName:@"SportViewController" vcTitle:LocalizedString(@"tab_sport") defaultImage:@"反射 (2)" selectedImage:@"反射 (1)"],
        [self createTabBarVCClassName:@"HardwareViewController" vcTitle:LocalizedString(@"tab_watch") defaultImage:@"watch (1)" selectedImage:@"手表 (1)"],
        [self createTabBarVCClassName:@"MeViewController" vcTitle:LocalizedString(@"tab_me") defaultImage:@"我的 (1)" selectedImage:@"我的 (1)"],
    ];
//    self.viewControllers = controllerList;
    [self updateTheme];
    
}


/**
 返回视图
 
 @param className 界面类名
 @param title 界面名称
 @param defaultImage 默认图片
 @param selectedImage 选中的图片
 @return vc
 */
- (UINavigationController *)createTabBarVCClassName:(NSString *)className vcTitle:(NSString *)title  defaultImage:(NSString *)defaultImage selectedImage:(NSString *)selectedImage{
    
    UIViewController *vc = [NSClassFromString(className) new];
//    vc.navigationItem.title = title;
    //    vc.tabBarItem.imageInsets = UIEdgeInsetsMake(6, 0, -6, 0);
    BaseNavigationViewController *navVC = [[BaseNavigationViewController alloc] initWithRootViewController:vc];
    navVC.tabBarItem.title = title;
    navVC.tabBarItem.image = [[UIImage imageNamed:defaultImage] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    navVC.tabBarItem.selectedImage = [[UIImage imageNamed:selectedImage] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    [self addChildViewController:navVC];
    return navVC;
}



- (void)updateTheme {
    
    //     适配iOS13导致的bug
    if (@available(iOS 13.0, *)) {
//        self.tabBar.unselectedItemTintColor = [UIColor lightGrayColor];
        UITabBarItem *item = [UITabBarItem appearance];
        [item setTitleTextAttributes:@{ NSForegroundColorAttributeName : [ThemeManager sharedInstance].themeValue == Theme_Default?S_HexColor(@"0x888888"):[UIColor lightTextColor],NSFontAttributeName:[UIFont boldSystemFontOfSize:10]} forState:UIControlStateNormal];
        [item setTitleTextAttributes:@{ NSForegroundColorAttributeName : [ThemeManager sharedInstance].themeValue == Theme_Default?[UIColor blackColor]:[UIColor whiteColor] ,NSFontAttributeName:[UIFont boldSystemFontOfSize:10]} forState:UIControlStateSelected];
    } else {
        // iOS 13以下
        [[UITabBarItem appearance] setTitleTextAttributes:@{ NSForegroundColorAttributeName : kTextColor,NSFontAttributeName:[UIFont boldSystemFontOfSize:10]}            forState:UIControlStateNormal];
        [[UITabBarItem appearance] setTitleTextAttributes:@{ NSForegroundColorAttributeName : [ThemeManager sharedInstance].themeValue == Theme_Default?[UIColor blackColor]:[UIColor whiteColor],NSFontAttributeName:[UIFont boldSystemFontOfSize:10]}            forState:UIControlStateSelected];
    }
    
//    if(@available(iOS 13.0, *)) {
//        UITabBarAppearance *appearance = [self.tabBar.standardAppearance copy];
//        appearance.backgroundImage = [UIImage imageWithColor:kUIColorFromRGB(0xffffff) size:CGSizeMake(kScreenWidth, kTabBarHeight)];
//        appearance.shadowImage = [UIImage imageWithColor:kUIColorFromRGB(0xffffff) size:CGSizeMake(kScreenWidth, kTabBarHeight)];
//        //下面这行代码最关键
//        [appearance configureWithTransparentBackground];
//        self.tabBar.standardAppearance = appearance;
//        
//    }else{
//        
//        [self.tabBar setBackgroundImage:[UIImage imageWithColor:kUIColo rFromRGB(0xffffff) size:CGSizeMake(kScreenWidth, kTabBarHeight)]];
//        [self.tabBar setShadowImage:[UIImage imageWithColor:kUIColorFromRGB(0xffffff) size:CGSizeMake(kScreenWidth, kTabBarHeight)]];
//        self.tabBar.translucent =YES;
//    }
    
    BaseNavigationViewController * vc1 = self.childViewControllers[0];
    BaseNavigationViewController * vc2 = self.childViewControllers[1];
    BaseNavigationViewController * vc3 = self.childViewControllers[2];
    BaseNavigationViewController * vc4 = self.childViewControllers[3];
    
    vc1.tabBarItem.title = LocalizedString(@"tab_home");
    vc2.tabBarItem.title = LocalizedString(@"tab_sport");
    vc3.tabBarItem.title = LocalizedString(@"tab_watch");
    vc4.tabBarItem.title = LocalizedString(@"tab_me");
    
    if ([ThemeManager  sharedInstance].themeValue == Theme_Default) {
        vc1.tabBarItem.selectedImage = [[UIImage imageNamed:@"首页"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        vc2.tabBarItem.selectedImage = [[UIImage imageNamed:@"反射"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        vc3.tabBarItem.selectedImage = [[UIImage imageNamed:@"watch (2)"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        vc4.tabBarItem.selectedImage = [[UIImage imageNamed:@"我的 (2)"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    }else {
        vc1.tabBarItem.selectedImage = [[UIImage imageNamed:@"首页 (1)"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        vc2.tabBarItem.selectedImage = [[UIImage imageNamed:@"反射 (1)"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        vc3.tabBarItem.selectedImage = [[UIImage imageNamed:@"watch"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        vc4.tabBarItem.selectedImage = [[UIImage imageNamed:@"我的"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    }
    
}

- (UIViewController *)sj_topViewController {
    if ( self.selectedIndex == NSNotFound )
        return self.viewControllers.firstObject;
    return self.selectedViewController;
}

- (BOOL)shouldAutorotate {
    return [[self sj_topViewController] shouldAutorotate];
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    return [[self sj_topViewController] supportedInterfaceOrientations];
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
    return [[self sj_topViewController] preferredInterfaceOrientationForPresentation];
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
